<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页面</title>
</head>

<body>
    <fieldset style="width:800px;margin:0 auto;">
        <legend>用户登录</legend>
        <form action="">
            <p><label for="">用户姓名：</label><input type="text" class="username"></p>
            <p><label for="">用户密码：</label><input type="password" class="password"></p>
            <p><input type="button" value="用户登录" class="btn"> <a href="registry.html">用户注册</a></p>
        </form>
    </fieldset>
</body>

</html>
<script src="js/ajax_promise.js"></script>
<script>
    const btn = document.querySelector('.btn');
    const username = document.querySelector('.username');
    const password = document.querySelector('.password');
    btn.onclick = function() {
        $ajax({
            type: 'post',
            url: 'http://localhost/vscodetwo/week-05/JD/set/php/login.php',
            data: {
                user: username.value,
                pass: password.value
            }
        }).then(function(data) { //data:后端返回的值
            if (data === 'true') { //登录成功:跳转首页，同时首页应该出现用户名等信息(本地存储)。
                window.localStorage.setItem('loginname', username.value);
                location.href = 'index.html';
            } else { //登录失败
                alert('用户名或者密码错误');
                password.value = ''; //清空密码
            }
        })
    }
</script>